﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Com.UmbracoChina.Consult.Models;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi;

namespace Com.UmbracoChina.Consult.Controllers
{
    [PluginController("MyOnlineConsult")]
    public class ConsultEndingController : UmbracoAuthorizedApiController
    {
        /// <summary>
        /// 返回全部咨询类别
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ConsultType> GetConsultType()
        {
            var types = new UmbracoHelper(UmbracoContext).TypedContentAtXPath("//homepage/consultingIndex/consultingList");
            List<ConsultType> result = new List<ConsultType>();
            if (types != null)
            {
                foreach (var item in types)
                {
                    result.Add(new ConsultType { nid = item.Id, name = item.Name });
                }
            }
            return result;
        }

        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public ConsultResult UpdateConsult(int id)
        {
            ConsultResult result = new ConsultResult() { errCode = -1, errMessage = "要更新的信息不存在" };

            string sqlStatement = "SELECT TOP 1 ID, Title, ConsultType, Email, ConsultContent, IP, IsSolve, AddTime FROM [OnlineConsolt] WHERE ID = " + id;
            OnlineConsult consult = PetaPocoHelper<OnlineConsult>.GetById(id, sqlStatement);
            if (consult != null && consult.ID > 0)
            {
                if (consult.IsSolve.Equals("n"))
                {
                    //1.导入Umbraco
                    int rootid = consult.ConsultType;
                    IContentService ics = UmbracoContext.Application.Services.ContentService;
                    IContent content = ics.CreateContent(consult.Title, rootid, "consultingDetail");

                    if (content.HasProperty("seoTitle"))
                    {
                        content.SetValue("seoTitle", consult.Title);
                    }
                    if (content.HasProperty("seoKeyword"))
                    {
                        content.SetValue("seoKeyword", consult.Title);
                    }
                    if (content.HasProperty("seoDescription"))
                    {
                        content.SetValue("seoDescription", consult.Title);
                    }


                    if (content.HasProperty("consultingQuestion"))
                    {
                        content.SetValue("consultingQuestion", consult.ConsultContent);
                    }
                    if (content.HasProperty("ip"))
                    {
                        content.SetValue("ip", consult.IP);
                    }
                    if (content.HasProperty("email"))
                    {
                        content.SetValue("email", consult.Email);
                    }
                    if (content.HasProperty("addingTime"))
                    {
                        content.SetValue("addingTime", consult.AddTime);
                    }
                    ics.Save(content);

                    //2.更新信息
                    consult.IsSolve = "y";
                    PetaPocoHelper<OnlineConsult>.Update(consult);

                    result.errCode = 0;
                    result.errMessage = content.Id.ToString();
                }
                else
                {
                    result.errMessage = "该信息已经导入";
                }
            }
            return result;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        
        public ConsultResult DeleteConsult(int id)
        {
            ConsultResult result = new ConsultResult() { errCode = -1 };
            try
            {
                PetaPocoHelper<SimpleOnlineConsult>.DeleteByID(id, "DELETE FROM [OnlineConsolt] WHERE ID = @0");
                result.errCode = 0;
                result.errMessage = "删除成功";
            }
            catch (Exception e)
            {
                UmbracoContext.Application.ProfilingLogger.Logger.Error(this.GetType(), e.Message, e);
                result.errCode = -1;
                result.errMessage = "出现异常，请联系管理员！";
            }
            return result;
        }

        /// <summary>
        /// 搜索信息
        /// </summary>
        /// <returns></returns>
        public IEnumerable<SimpleOnlineConsult> SearchConsult()
        {
            string sqlStatement = "SELECT ID, Title, ConsultType, Email, IP, IsSolve, AddTime FROM [OnlineConsolt] ";
            sqlStatement += " ORDER BY ConsultType ASC, AddTime DESC";
            return PetaPocoHelper<SimpleOnlineConsult>.GetAll(sqlStatement);
        }
    }
}
